using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_HeroListCell : UIBaseNode
    {

        #region Template Generate,don't modify
        protected partial class UIB_UIN_HeroListCell
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExButton heroListCell { protected set; get; }
            public IQIGame.Onigao.Framework.ExArtSpriteNum classTypeIcon { protected set; get; }
            public IQIGame.Onigao.Framework.ExRawImage icon { protected set; get; }
            public UnityEngine.GameObject baseInfo { protected set; get; }
            public UnityEngine.GameObject lv { protected set; get; }
            public UnityEngine.GameObject star { protected set; get; }
            public UnityEngine.GameObject selectRoot { protected set; get; }
            public UnityEngine.GameObject lockRoot { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("heroListCell", out var __tbv0);
                this.heroListCell = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExArtSpriteNum>("classTypeIcon", out var __tbv1);
                this.classTypeIcon = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExRawImage>("icon", out var __tbv2);
                this.icon = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("baseInfo", out var __tbv3);
                this.baseInfo = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("lv", out var __tbv4);
                this.lv = __tbv4;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("star", out var __tbv5);
                this.star = __tbv5;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("selectRoot", out var __tbv6);
                this.selectRoot = __tbv6;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("lockRoot", out var __tbv7);
                this.lockRoot = __tbv7;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        ListScrollAdapter<UIN_CommonDarkLight> listStar = null;
        UIN_CommonTitleValue lvTitVal = null;

        public HeroData heroData { get; private set; } = null;

        protected UIB_UIN_HeroListCell ui { get; set; }
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_HeroListCell();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.listStar = this.InitListScroll<UIN_CommonDarkLight>(this.ui.star, this.OnStarChanged);
            this.lvTitVal = this.InitChildNode<UIN_CommonTitleValue>(this.ui.lv);

            this.ui.heroListCell.onClick.AddListener(this.OnClickSelf);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {
            this.heroData = null;
        }

        public void Show(HeroData data)
        {
            this.heroData = data;
            if (this.heroData == null)
                return;
            this.Show();
            CfgHero cfgData = this.heroData.cfgData;
            bool isHas = this.heroData.isGeted;

            int skinCid = this.heroData.skinCid;
            CfgHeroSkin cfgSkin = TableCenter.heroSkin.Get(skinCid);
            this.SetRawImageTexture(this.ui.icon, cfgSkin.HeroHalfBodyImage_fullPath);
            this.ui.classTypeIcon.SetInt(cfgData.HeroClass);
            this.listStar.RefillCells(0);
            this.lvTitVal.Hide();
            // this.ui.heroListCell.text = "";
            this.ui.heroListCell.text = HeroModule.Instance.GetHeroName(this.heroData);
            if (isHas)
            {
                this.listStar.RefillCells(APIUIConst.HeroDefautMaxStar);
                string strTitle = APIUIConst.GetLvStr("");
                string strValue = this.heroData.level.ToString();
                this.lvTitVal.Show(strValue, strTitle);
            }
            this.ui.lockRoot.SetActive(!isHas);
        }

        void OnStarChanged(UIN_CommonDarkLight item, int index)
        {
            int nCurr = index;
            int bigStar = -1;
            if (this.heroData.cfgStar != null)
            {
                bigStar = this.heroData.cfgStar.BigStar - 1;
            }
            bool isLight = (bigStar >= nCurr);
            item.Show(isLight);
        }

        protected virtual void OnClickSelf()
        {
            UD_HeroInfo ud = UD_HeroInfo.NewFromPool().Sync(this.heroData, true);
            this.OpenUI<UIHeroInfo>(ud);
        }
    }
}
